package com.sugon.controller;

import com.sugon.entity.ToolsRecordEntity;
import com.sugon.service.ToolsRecordService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 工装台账Controller
 *
 * @author sugon
 * @date 2019-07-19 11:16:57
 */
@Controller
@RequestMapping("toolsrecord")
public class ToolsRecordController {
    @Autowired
    private ToolsRecordService toolsRecordService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("toolsrecord:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ToolsRecordEntity> toolsRecordList = toolsRecordService.queryList(query);
        int total = toolsRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("toolsrecord:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsRecordEntity toolsRecord = toolsRecordService.queryObject(id);

        return R.ok().put("toolsRecord", toolsRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolsrecord:save")
    @ResponseBody
    public R save(@RequestBody ToolsRecordEntity toolsRecord) {
        toolsRecordService.save(toolsRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("toolsrecord:update")
    @ResponseBody
    public R update(@RequestBody ToolsRecordEntity toolsRecord) {
        toolsRecordService.update(toolsRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolsrecord:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsRecordService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsRecordEntity> list = toolsRecordService.queryList(params);

        return R.ok().put("list", list);
    }
}
